﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    public class UserManager : IUserManager
    {
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="users">登录信息</param>
        /// <returns></returns>
        public OperResult Register(TBLogins login)
        {
            if (string.IsNullOrWhiteSpace(login.UserName))
            {
                return OperResult.Failed("用户名不能为空！");
            }
            if (string.IsNullOrWhiteSpace(login.MobileNumber))
            {
                return OperResult.Failed("手机号不能为空！");
            }
            if (string.IsNullOrWhiteSpace(login.Password))
            {
                return OperResult.Failed("密码不能为空！");
            }
            // 判断用户名是否已注册
            IUserService userService = new UserService();
            if (userService.ExistUserName(login.UserName))
            {
                return OperResult.Failed("用户名已注册！");
            }
            // 判断手机号是否已注册
            if (userService.ExistMobileNumber(login.MobileNumber))
            {
                return OperResult.Failed("手机号已注册！");
            }
            // 加密用户密码
            SHAEncryption shaEncryption = new SHAEncryption();
            login.Password = shaEncryption.SHA1Encrypt(login.Password);
            // 注册用户
            try
            {
                userService.Register(login);
            }
            catch (Exception ex)
            {
                return OperResult.Wrong(ex);
            }
            // 返回结果
            return OperResult.Succeed();
        }

        /// <summary>
        /// 通过用户名和密码登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public OperResult<TBUsers> LoginByUserName(string userName, string password)
        {
            //判断用户名和密码是否为空
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("用户名或密码不能为空！");
            }

            //通过用户名和密码查找用户
            IUserService userService = new UserService();
            TBLogins loginEntity = userService.FindByUserName(userName);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在");
            }

            //验证用户的密码是否需正确
            SHAEncryption sHAEncryption = new SHAEncryption();
            //currentPwd 当前页面输入的密码进行加密
            string currentPwd = sHAEncryption.SHA1Encrypt(password);
            if (!currentPwd.Equals(loginEntity.Password))
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }

            //查找用户详细信息
            TBUsers user = userService.FindByUserId(loginEntity.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

        /// <summary>
        /// 获取用户信息的详细信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public OperResult<TBUsers> GetUser(int userId)
        {
            IUserService userService = new UserService();
            TBUsers user = userService.FindByUserId(userId);
            return OperResult<TBUsers>.Succeed(user);
        }

        public OperResult<TBUsers> LoginByMobile(string mobile, string password)
        {
            //判断用户名和密码是否为空
            if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUsers>.Failed("手机号或密码不能为空！");
            }

            //通过手机号和密码查找用户
            IUserService userService = new UserService();
            TBLogins loginEntity = userService.FindByMobile(mobile);
            if (loginEntity == null)
            {
                return OperResult<TBUsers>.Failed("该用户不存在");
            }

            //验证用户的密码是否需正确
            SHAEncryption sHAEncryption = new SHAEncryption();
            //currentPwd 当前页面输入的密码进行加密
            string currentPwd = sHAEncryption.SHA1Encrypt(password);
            if (!currentPwd.Equals(loginEntity.Password))
            {
                return OperResult<TBUsers>.Failed("密码错误！");
            }

            //查找用户详细信息
            TBUsers user = userService.FindByUserId(loginEntity.Id);
            return OperResult<TBUsers>.Succeed(user);
        }

        /// <summary>
        /// 更新用户信息(不包含头像)
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        public OperResult<TBUsers> EditUser(TBUsers user)
        {
            IUserService userService = new UserService();
            try
            {
                TBUsers profileEntity = userService.EditUser(user);
                return OperResult<TBUsers>.Succeed(profileEntity);
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Wrong(ex);
            }
        }

        /// <summary>
        /// 编辑头像
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns></returns>
        public OperResult<TBUsers> EditAvatar(TBUsers user)
        {
            IUserService userService = new UserService();
            try
            {
                TBUsers profileEntity = userService.EditAvatar(user);
                return OperResult<TBUsers>.Succeed(profileEntity);
            }
            catch (Exception ex)
            {
                return OperResult<TBUsers>.Wrong(ex);
            }
        }

        public List<TBUsers> GetList()
        {
            IUserService userService = new UserService();
            return userService.GetList();
        }
    }
}
